package com.smarttest.portal.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smarttest.common.beans.GeneralResultWithStatus;
import com.smarttest.common.util.MsgSend;
import com.smarttest.portal.pojo.DbAutoSetting;
import com.smarttest.portal.service.AutoSetting;
@RequestMapping("/autosetting")
@Controller
public class AutoSettingController {
	@Autowired
	AutoSetting AutoSetting;
	@RequestMapping(value = "/modifySetting.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus modifySetting(int envid, Integer moduleid, int type, String setting) {	
		return 	GeneralResultWithStatus.okData(null, AutoSetting.modifySetting(envid, moduleid, type, setting));	
	}
	@RequestMapping(value = "/getSetting.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus getSetting(int envid, Integer moduleid, int type) {	
		//获取数据库自动化配置，然后组装成html代码在前端展示
		List<DbAutoSetting> settinglist = AutoSetting.getSetting(envid, moduleid, type);
		List<Map<String,String>> l = new ArrayList<Map<String,String>>();
		if (settinglist.size() == 1) {
			JSONArray setarray = JSON.parseArray(settinglist.get(0).getSetting());
			for (Object jo : setarray) {
				Map<String,String> m = new HashMap<String, String>();
				String datatype = ((JSONObject)jo).getString("type");
				String tabledata = "";
				JSONArray datas = ((JSONObject)jo).getJSONArray("data");
				for (Object trdata : datas) {
					JSONObject data = ((JSONObject)trdata);
					if (datatype.equalsIgnoreCase("message")) {
						String msg_sendmethod = data.getString("msg_sendmethod");
						String msg_add = data.getString("msg_add");
						Integer msg_port = data.getInteger("msg_port");
						String msg_sendtime = data.getString("msg_sendtime");
						String msg_format = data.getString("msg_format");
						Integer msg_internal = data.getInteger("msg_internal");
						String msg_text = data.getString("msg_text");
						String msg_startnum = data.getString("msg_startnum");
						String msg_count = data.getString("msg_count");
						String msg_key = data.getString("msg_key");
						tabledata+="<tr><td align=\"center\"><input type=\"checkbox\"></td>"+
								"<td class=\"editable\">"+msg_sendmethod+"</td>"+
								"<td class=\"editable\">"+msg_add+"</td>"+
								"<td class=\"editable\">"+msg_port+"</td>"+
								"<td class=\"editable\">"+msg_sendtime+"</td>"+
								"<td class=\"editable\">"+msg_format+"</td>"+
								"<td class=\"editable\">"+msg_internal+"</td>"+
								"<td class=\"editable\">"+msg_text+"</td>"+
								"<td class=\"editable\">"+msg_startnum+"</td>"+
								"<td class=\"editable\">"+msg_count+"</td>"+
								"<td class=\"editable\">"+msg_key+"</td>"+
								"</tr>";
					}
					else if (datatype.equalsIgnoreCase("mysql")) {
									
								}
					else if (datatype.equalsIgnoreCase("rocketmq")) {
						
					}
					else if (datatype.equalsIgnoreCase("mongo")) {
						
					}
				}
				m.put(datatype, tabledata);
				l.add(m);
			}
		}
		return 	GeneralResultWithStatus.okData(null, l);	
	}
}